package cn.seocoo.platform.advertisement.service.impl;

import java.util.Date;
import java.util.List;

import cn.seocoo.platform.advertisement.dto.AdvertisementDto;
import cn.seocoo.platform.common.constant.ClassContants;
import cn.seocoo.platform.common.utils.AmountUtils;
import com.github.pagehelper.PageHelper;
import com.jpay.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.seocoo.platform.advertisement.mapper.AdvertisementMapper;
import cn.seocoo.platform.advertisement.domain.Advertisement;
import cn.seocoo.platform.advertisement.service.IAdvertisementService;
import cn.seocoo.platform.common.core.text.Convert;

/**
 * 广告 服务层实现
 * 
 * @author wujian
 * @date 2020-01-05
 */
@Service
public class AdvertisementServiceImpl implements IAdvertisementService 
{
	@Autowired
	private AdvertisementMapper advertisementMapper;

	/**
     * 查询广告信息
     * 
     * @param id 广告ID
     * @return 广告信息
     */
    @Override
	public Advertisement selectAdvertisementById(Integer id)
	{
	    return advertisementMapper.selectAdvertisementById(id);
	}
	
	/**
     * 查询广告列表
     * 
     * @param advertisement 广告信息
     * @return 广告集合
     */
	@Override
	public List<Advertisement> selectAdvertisementList(Advertisement advertisement)
	{
	    return advertisementMapper.selectAdvertisementList(advertisement);
	}
	
    /**
     * 新增广告
     * 
     * @param advertisement 广告信息
     * @return 结果
     */
	@Override
	public int insertAdvertisement(Advertisement advertisement)
	{
		advertisement.setAuditStatus(ClassContants.WAIT_AUDIT);
		advertisement.setDelFlag(ClassContants.NO_DEL_FLAG);
		advertisement.setAmount(Integer.valueOf(AmountUtils.changeY2F(advertisement.getAmountDto())));
		advertisement.setIsRecommend(ClassContants.NO_RECOMMEND);
		advertisement.setCreateTime(new Date());
	    return advertisementMapper.insertAdvertisement(advertisement);
	}
	
	/**
     * 修改广告
     * 
     * @param advertisement 广告信息
     * @return 结果
     */
	@Override
	public int updateAdvertisement(Advertisement advertisement)
	{
	    return advertisementMapper.updateAdvertisement(advertisement);
	}

	/**
     * 删除广告对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteAdvertisementByIds(String ids)
	{
		return advertisementMapper.deleteAdvertisementByIds(Convert.toStrArray(ids));
	}

	/**
	 * @Author WuJian
	 * @Description //TODO 我的商铺中广告列表
	 * @Date 21:39 2020/1/6
	 * @Param [appUserId, pageSize, pageNum]
	 * @return java.util.List<cn.seocoo.platform.advertisement.dto.AdvertisementDto>
	 **/
	@Override
	public List<AdvertisementDto> getUserAdvertisement(Integer appUserId, Integer pageSize, Integer pageNum) {
		//开启分页
		PageHelper.startPage(pageNum == null? 1:pageNum,pageSize == null?10:pageSize);
		List<AdvertisementDto> list = advertisementMapper.getUserAdvertisement(appUserId);

		return list;
	}

	/**
	 * @Author WuJian
	 * @Description //筛选页面中的广告列表
	 * @Date 21:50 2020/1/6
	 * @Param []
	 * @return java.util.List<cn.seocoo.platform.advertisement.dto.AdvertisementDto>
	 **/
	@Override
	public List<AdvertisementDto> getAdvertiseForApi() {
		List<AdvertisementDto> advertiseForApi = advertisementMapper.getAdvertiseForApi();
		if(advertiseForApi != null && advertiseForApi.size() >0){
			for (AdvertisementDto advertisementDto : advertiseForApi) {
				if(advertisementDto.getAmount() != null){
					String price = AmountUtils.changeF2W(advertisementDto.getAmount().toString());
					System.out.println(price);
					advertisementDto.setPrice(price);
				}
			}
		}
		return advertiseForApi;
	}

}
